package pe.gob.mef.snip.preinversion.controller;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.servlet.ModelAndView;

import pe.gob.mef.snip.preinversion.domain.TakeaNumber;
import pe.gob.mef.snip.preinversion.service.TakeaNumberService;
import pe.gob.mef.snip.preinversion.util.Parametros;

@Controller
public class PrincipalController {

	protected final Log log = LogFactory.getLog(getClass());
	
	@Autowired	
	private TakeaNumberService takeaNumberService;	
	
	private TakeaNumber takeaNumber;
	
	@RequestMapping(value = "/principalSnip03", method = RequestMethod.GET )
	public ModelAndView dirigirPrincipalSnip03(HttpServletRequest request, HttpServletResponse response) {

		if (log.isDebugEnabled()) log.debug("estudioSnip03Listar(HttpServletRequest,HttpServletResponse");
		
		Map<String, Object> principalSnip03Map = new HashMap<String, Object>();	
		String view = "frmSnip03";
		HttpSession session = request.getSession();	
		String idProyecto = (String) session.getAttribute("idProyecto");		
		//String idProyecto = (String) request.getAttribute("idProyecto");
		String accion = (String) request.getParameter("accion");
		System.out.println("accion: " + accion );
		System.out.println("httpsession: " + idProyecto );		
		String valor = (String) RequestContextHolder.currentRequestAttributes().getAttribute("idProyecto", RequestAttributes.SCOPE_SESSION);
		System.out.println("RequestContextHolder: " + idProyecto );
		
		if(accion.equalsIgnoreCase("registrar")) {			
			if(idProyecto == null) {
				takeaNumber = new TakeaNumber();
				takeaNumber.setId((Short)Short.parseShort(Parametros.IDTABLA_PROYECTO_BANCO));
				takeaNumberService.obtenerNextNumber(takeaNumber);	
				session.setAttribute("idProyecto", takeaNumber.getNextnumber().toString());
				principalSnip03Map.put("idProyecto",takeaNumber.getNextnumber().toString());
				System.out.println("Primera vez: [ idProyecto = " + takeaNumber.getNextnumber().toString() + " ]");				
			}
			else {			
				System.out.println("Segunda vez: [ idProyecto = " + idProyecto + " ]");
			}
			
		} else {
			
		}
		principalSnip03Map.put("accion", accion);	
		return new ModelAndView(view,"principalSnip03Map",principalSnip03Map);
	}
	
	@RequestMapping("/principalSnip04")
	public ModelAndView dirigirPrincipalSnip04(HttpServletRequest request, HttpServletResponse response) {
		
		if (log.isDebugEnabled()) log.debug("estudioSnip04Listar(HttpServletRequest,HttpServletResponse");
		
		Map<String, Object> principalSnip04Map = new HashMap<String, Object>();	
		String view = "frmSnip04";
		HttpSession session = request.getSession();	
		String idProyecto = (String) session.getAttribute("idProyecto");		
		//String idProyecto = (String) request.getAttribute("idProyecto");
		String accion = (String) request.getParameter("accion");		
		System.out.println("httpsession: " + idProyecto );		
		String valor = (String) RequestContextHolder.currentRequestAttributes().getAttribute("idProyecto", RequestAttributes.SCOPE_SESSION);
		System.out.println("RequestContextHolder: " + idProyecto );
		
		if(accion.equalsIgnoreCase("registrar")) {			
			if(idProyecto == null) {
				takeaNumber = new TakeaNumber();
				takeaNumber.setId((Short)Short.parseShort(Parametros.IDTABLA_PROYECTO_BANCO));
				takeaNumberService.obtenerNextNumber(takeaNumber);	
				session.setAttribute("idProyecto", takeaNumber.getNextnumber().toString());
				principalSnip04Map.put("idProyecto",takeaNumber.getNextnumber().toString());
				System.out.println("Primera vez: [ idProyecto = " + takeaNumber.getNextnumber().toString() + " ]");				
			}
			else {			
				System.out.println("Segunda vez: [ idProyecto = " + idProyecto + " ]");
			}
			
		} else {
			
		}
		principalSnip04Map.put("accion", accion);
		
		return new ModelAndView(view,"identificacionSnip04Map",principalSnip04Map);
	}
	


}
